package red.mlz.gameservice.controller.game;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import red.mlz.common.entity.game.Game;
import red.mlz.gameservice.service.game.GameService;

import java.util.List;

@RestController
@RequestMapping("")
@Slf4j
public class GameController {
    @Autowired
    private GameService gameService;

    @RequestMapping("/game-service/games")
    public List<Game> getGames(@RequestParam(name = "page")Integer page,
                                @RequestParam(name = "pageSize") Integer pageSize,
                                @RequestParam(name = "keyword", required = false) String keyword){
        return gameService.getAllGames(page, pageSize, keyword);
    }

    @RequestMapping("/game-service/game/{id}")
    public Game getGameById(@PathVariable("id") Long id){
        return gameService.getById(id);
    }

    @RequestMapping("/game-service/game/add")
    public String addGame(@RequestParam(name = "gameId", required = false) Long gameId,
                          @RequestParam(name = "gameName") String gameName,
                          @RequestParam(name = "playerNumber") Long playerNumber,
                          @RequestParam(name = "title") String title,
                          @RequestParam(name = "coverImages") String coverImages,
                          @RequestParam(name = "releaseDate") String releaseDate,
                          @RequestParam(name = "price") Float price,
                          @RequestParam(name = "categoryId") Long categoryId,
                          @RequestParam(name = "gameDescription") String gameDescription,
                          @RequestParam(name = "rating") Float rating,
                          @RequestParam(name = "tags",required = false)String tags) {

        try {
            Long resultGameId = gameService.edit(gameId, gameName, playerNumber,
                    title, coverImages, releaseDate, price, categoryId, gameDescription, rating,tags);
            return resultGameId.toString();
        } catch (Exception e) {
            return e.getMessage();
        }
    }

    @RequestMapping("/game-service/game/update")
    public String updateGame(@RequestParam(name = "gameId") Long gameId,
                               @RequestParam(name = "gameName") String gameName,
                               @RequestParam(name = "playerNumber") Long playerNumber,
                               @RequestParam(name = "title") String title,
                               @RequestParam(name = "coverImages") String coverImages,
                               @RequestParam(name = "releaseDate") String releaseDate,
                               @RequestParam(name = "price") Float price,
                               @RequestParam(name = "categoryId") Long categoryId,
                               @RequestParam(name = "gameDescription") String gameDescription,
                               @RequestParam(name = "rating") Float rating,
                               @RequestParam(name = "tags",required = false)String tags) {

        try {

            Long GameId = gameService.edit(gameId, gameName.trim(), playerNumber,
                    title.trim(), coverImages, releaseDate.trim(), price,
                    categoryId, gameDescription, rating,tags);
            return GameId.toString();
        }catch (Exception e) {
            return e.getMessage();
        }
    }

    @RequestMapping("/game-service/game/delete")
    public String deleteGame(@RequestParam(name = "gameId", required = false) Long gameId) {

                // 删除游戏
                Long isDeleted = gameService.delete(gameId);
                if (isDeleted == 0) {
                    return"未找到对应游戏，删除失败";
                }
                return "游戏删除成功";
    }

    @RequestMapping("/game-service/gettotalgames")
    public Integer getTotalGames(){
        return gameService.getTotalGames();
    }

    @RequestMapping("/game-service/game/edit")
    public Long editGame(@RequestParam(name = "gameId", required = false) Long gameId,
                         @RequestParam(name = "gameName") String gameName,
                         @RequestParam(name = "playerNumber") Long playerNumber,
                         @RequestParam(name = "title") String title,
                         @RequestParam(name = "coverImages") String coverImages,
                         @RequestParam(name = "releaseDate") String releaseDate,
                         @RequestParam(name = "price") Float price,
                         @RequestParam(name = "categoryId") Long categoryId,
                         @RequestParam(name = "gameDescription") String gameDescription,
                         @RequestParam(name = "rating") Float rating,
                         @RequestParam(name = "tags",required = false)String tags){
        return gameService.edit(gameId, gameName, playerNumber,
                title, coverImages, releaseDate, price, categoryId, gameDescription, rating,tags);
    }

    @RequestMapping("/game-service/game/deleted")
    public Long delete (@RequestParam(name = "gameId")Long gameId){
        return gameService.delete(gameId);
    }



}
